package com.commonlib.downloadmgr.subclass;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;

import com.commonlib.downloadmgr.base.DownloadBaseJob;
import com.commonlib.downloadmgr.base.DownloadBaseManager;
import com.commonlib.downloadmgr.db.EntityDatabaseBuilder;
import com.commonlib.downloadmgr.entity.Entity;

public class DownloadManager extends DownloadBaseManager<Entity>{
//	private static final String TAG = DownloadManager.class.getSimpleName();
	private static final int MAX_TASK = 2;
	public static final String PREF_NAME = "pref_name";
	public static final String EXTRA_NAME = "extra_name";
	
	private static DownloadManager self;
	
	public static DownloadManager getInstance(Context context){
		if (self == null) {
			self = new DownloadManager(context, MAX_TASK);
		}
		return self;
	}
	
	private DownloadManager(Context context, int maxTask){
		super(context, maxTask);
	}
	
	@Override
	public long updateData(final DownloadBaseJob<Entity> job) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				EntityDatabaseBuilder.getInstance(context).saveDownloadJob(job);
			}
		}).start();
		return -1;
//		return DatabaseImplEntity.getInstance(context).saveDownloadJob(job);
	}

	@Override
	public DownloadBaseJob<Entity> queryData(final String id) {
		return EntityDatabaseBuilder.getInstance(context).getDownloadJob(id);
//		return null;
	}
	
	@Override
	public long deleteData(final String id) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				EntityDatabaseBuilder.getInstance(context).deleteDownloadJob(id);
			}
		}).start();
		return -1;
//		return DatabaseImplEntity.getInstance(context).deleteDownloadJob(id);
	}

	@Override
	public ArrayList<DownloadBaseJob<Entity>> readData() {
//		return null;
		return EntityDatabaseBuilder.getInstance(context).getAllDownloadJobs();
	}

	@Override
	protected Intent getIntentDownoadService() {
		return new Intent(this.context, DownloadService.class);
	}

}
